package dnbW.uiW.attribpickW;

import dnbW.dbW.DataSource;
import dnbW.dbW.sucxentdbW.EntityManager;
import dnbW.dbW.sucxentdbW.EntityQueryProcessor;
import dnbW.dbW.sucxentdbW.SucxentDataSource;
import dnbW.entityW.Attribute;
import dnbW.entityW.Entity;
import dnbW.entityW.IEntity;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Collection;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import javax.swing.Timer;

/**
 * The controller of the package
 * @author Truong Ba Quan
 */
public class AttribPickController {
    private DataSource dataSource;

    private EntityManager entityManager;

    private AttributeIterator iter;
    
    //private AttribPickDialog dialog;

    private EntityQueryProcessor queryProcessor;

    public AttribPickController(DataSource dataSource) {
        this.dataSource = dataSource;
        this.entityManager = ((SucxentDataSource)dataSource).getEntityManager();
        //this.dialog = new AttribPickDialog(DNB.getApplication().getMainFrame(), true, this);
        this.queryProcessor = new EntityQueryProcessor((SucxentDataSource)this.dataSource);
    }

    public void start() {
        Entity[] entities = new Entity[entityManager.getEntities().size()];
        entities = entityManager.getEntities().toArray(entities);

        Entity entity;
        entity = entities[1];
      //  dialog.addEntity(entity);
        this.shiftEntity(entity);
        
      //  this.dialog.setVisible(true);

        if (this.iter != null) {
            iter.stop();
        }
        
    }

    private void displayEntityLocality(IEntity entity) {
        for (Attribute attribute : entity.getAttributes()) {
            System.out.println(attribute.getElementAtLevel(attribute.getLevel()));
        }
        System.out.println();

        if (this.iter != null) {
            iter.stop();
        }
        this.iter = new AttributeIterator(entity.getAttributes());

        for (IEntity relatedEntity : entity.getRelatedEntities()) {
            //this.dialog.addRelatedEntity(entity, relatedEntity);
        }

        this.iter.start();
    }

    protected void shiftEntity(IEntity entity) {
//        IEntity kept = (IEntity)this.dialog.cleanScene(entity);
 //       if (!entity.equals(kept)) this.dialog.addEntity(entity);
        this.displayEntityLocality(entity);
    }

    public void search(String keyword) {
        IEntity entity = this.queryProcessor.getRelevantEntity(keyword);
        if (entity != null) {
            this.shiftEntity(entity);
        }
    }

    private class AttributeIterator implements ActionListener {
        private Iterator<Attribute> iter;
        private Timer timer;

        public AttributeIterator(Collection<Attribute> collection) {
            this.iter = collection.iterator();
            this.timer = new Timer(1000, this);
        }

        public void start() { this.timer.start(); }

        public void stop() { this.timer.stop(); }

        @Override
        public void actionPerformed(ActionEvent e) {
            if (iter.hasNext()) {
                Attribute attrib = iter.next();
                //dialog.addAttribute(attrib);
                System.out.println("Attribute " + attrib.getPath() + " shown");
            }
        }
    }

}
